<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 接收消息类型接口
 */
class Type extends Api
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Type();
    }

    //用户设置商机类型
    public function userset()
    {
        if ($this->request->isPost()) {

            $type_ids = $this->request->request('type_ids');
            if (!$type_ids) $this->error('参数错误');
            $typeid_arr = explode(',', $type_ids);

            //设置之前先清空
            Db::name('message_type')->where(['user_id' => $this->auth->id])->delete();

            foreach ($typeid_arr as $k => $v) {
                $res = Db::name('message_type')->insert(['user_id' => $this->auth->id, 'type_id' => $v]);
            }

            if ($res) {
                $this->success('设置成功');
            } else {
                $this->error('设置失败');
            }

        } else {
            $messageTypeM = new \app\common\model\MessageType();
            $data = $messageTypeM->where(['user_id' => $this->auth->id])->select();

            $this->success('',$data);
        }

    }

}
